package com.ias.agdis_r.manager;

import android.content.Context;
import android.os.RemoteException;

/*
import com.iflytek.speech.ISpeechModule;
import com.iflytek.speech.InitListener;
import com.iflytek.speech.SpeechConstant;
import com.iflytek.speech.SpeechSynthesizer;
import com.iflytek.speech.SynthesizerListener;
 */

import java.util.Iterator;
import java.util.List;

public class TTSManager {

    private static TTSManager instance = null;
    //private SpeechSynthesizer mTts;
    public static String SPEAKER = "speaker";
    private boolean isTtsOrder = false;      // tts语音播报是否顺序播放

    private TTSManager() {
        initTTSOrder();
    }

    public static TTSManager getInstance() {
        if (null == instance) {
            instance = new TTSManager();
        }
        return instance;
    }

    public void initTTS(Context context) {
        /*
        if (null == mTts) {
            mTts = new SpeechSynthesizer(context, mTtsInitListener);
            setParam();
        }
         */
    }

    /** 初始化语音时候顺序播报 */
    public void initTTSOrder() {
//        isTtsOrder = DataBaseManager.getInstance().getBoolean(ParameterModel.T_KEYPRESS, ParameterModel.TTS_ORDER);
    }

    /** 初始化监听
    private InitListener mTtsInitListener = new InitListener() {
        @Override
        public void onInit(ISpeechModule arg0, int code) {
//            if (code == ErrorCode.SUCCESS)
        }
    };
     */

    /** 合成回调监听
    private SynthesizerListener mTtsListener = new SynthesizerListener.Stub() {
        @Override
        public void onBufferProgress(int progress) throws RemoteException {

        }

        @Override
        public void onCompleted(int code) throws RemoteException {
            if (!isTtsOrder) {     // 关闭tts顺序播报
                return;
            }
            List<String> listMessage = TTSMessageTask.getInstance().getListMessage();
            Iterator<String> iterator = listMessage.iterator();
            if (iterator.hasNext()) {
                iterator.next();
                iterator.remove();
            }
            TTSMessageTask.getInstance().notifyTTS();
        }

        @Override
        public void onSpeakBegin() throws RemoteException {

        }

        @Override
        public void onSpeakPaused() throws RemoteException {

        }

        @Override
        public void onSpeakProgress(int progress) throws RemoteException {

        }

        @Override
        public void onSpeakResumed() throws RemoteException {

        }
    };

    private void setParam() {
        if (mTts != null) {
            mTts.setParameter(SpeechConstant.ENGINE_TYPE, "local");
            mTts.setParameter(SpeechSynthesizer.VOICE_NAME, "xiaoyan");
            mTts.setParameter(SpeechSynthesizer.SPEED, "50");
            mTts.setParameter(SpeechSynthesizer.PITCH, "50");
            mTts.setParameter(SpeechSynthesizer.VOLUME, "50");
        }
    }
     */

    public void ttsPlay(String tts) {
        if (isTtsOrder) {
            TTSMessageTask.getInstance().addMessage(tts);
            return;
        }
        ttsPlayReally(tts);
    }

    public void ttsPlayReally(String tts) {
        /*
        if (mTts != null) {
            mTts.startSpeaking(tts, mTtsListener);
        }
         */
    }

    public void ttsCancel() {
        if (isTtsOrder) {
            TTSMessageTask.getInstance().closeMessage();
            return;
        }
        ttsCancelReally();
    }

    public void ttsCancelReally() {
        /*
        if (mTts != null && mTts.isSpeaking()) {
            mTts.stopSpeaking(mTtsListener);
        }
         */
    }

    public void ttsPause() {
        /*
        if (mTts != null) {
            mTts.pauseSpeaking(mTtsListener);
        }
         */
    }

    public void ttsResume() {
        /*
        if (mTts != null) {
            mTts.resumeSpeaking(mTtsListener);
        }
         */
    }

    public void ttsSet() {
//		String speaker = mTts.getParameter(SpeechSynthesizer.LOCAL_SPEAKERS);
//		Intent intent = new Intent(context, TtsSettings.class);
//		intent.putExtra(SPEAKER, speaker);
//		context.startActivity(intent);
    }

    public void onDestory() {
        /*
        if (mTts != null) {
            mTts.stopSpeaking(mTtsListener);
            mTts.destory();
        }
         */
    }
}
